package org.jpass.dc.business.admin.logic;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.jpass.dc.business.admin.bean.AdminBean;
import org.jpass.dc.business.admin.dao.AdminDao;
import org.jpass.dc.common.bean.Page;
import org.jpass.dc.common.exception.ApplicationException;
import org.jpass.dc.common.util.Encrypter;
import org.jpass.dc.common.util.RefParaValue;

public class AdminLogic {
	private AdminDao adminDao = new AdminDao();

	/**
	 * 管理员登录
	 * @param name 管理员名称
	 * @param password 管理员密码
	 * @return
	 */
	public AdminBean login(String name,String password) throws Exception {
		AdminBean admin=null;
		String passwordVar = Encrypter.md5Encrypt(password);
		try{
			admin = adminDao.findAdminByName(name);
			if(!RefParaValue.empty(admin)){
				if(passwordVar.equals(admin.getPassword())
						|| password.equals(admin.getPassword())){
					int loginTimes = admin.getLoginTimes();
					if(loginTimes==0){ //修改登录次数
						admin.setLoginTimes(1);
					}else{
						admin.setLoginTimes(loginTimes+1);
					}
					admin.setIsOnline(1);
					if(admin.getThisSignTime()!=null){
						admin.setLastSignTime(admin.getThisSignTime());
					}
					admin.setThisSignTime(new Date()); //设置登录时间
					adminDao.updateAdmin(admin);
					return admin;
				}else{
					throw new ApplicationException("密码错误！");
				}
			}else{
				throw new ApplicationException("不存在此管理员！");
			}
		}catch(SQLException e){
			throw new ApplicationException("管理员登录时发生数据库操作错误！");
		}
	}

	/**
	 * 创建管理员
	 * @param name 管理员名称
	 * @param password 管理员密码
	 * @return
	 */
	public boolean addAdmin(AdminBean adminVar) throws Exception {
		AdminBean admin=null;
		try{
			admin = adminDao.findAdminByName(adminVar.getName());
			if(RefParaValue.empty(admin)){
				adminDao.addAdmin(adminVar);
			}else{
				throw new ApplicationException("此管理员已存在！");
			}
		}catch(SQLException e){
			throw new ApplicationException("管理员登录时发生数据库操作错误！");
		}
		return true;
	}
	
	public void updateLoginTime(AdminBean admin) throws Exception {
		admin.setLastSignTime(new Date()); //设置登录时间
		adminDao.updateAdmin(admin);
	}

	/**
	 * 管理员密码修改
	 * @param name 管理员名称
	 * @param oldPassword 管理员旧密码
	 * @param newPassword 管理员新密码
	 * @return
	 */
	public boolean updatePass(String id, String oldPassword, String newPassword) throws Exception {
		AdminBean admin = null;
		String oldPasswordVar = Encrypter.md5Encrypt(oldPassword);
		String newPasswordVar = Encrypter.md5Encrypt(newPassword);
		try{
			admin = adminDao.findAdminById(id);
			if(!RefParaValue.empty(admin)){ 
				if(admin.getPassword().equals(oldPasswordVar)||admin.getPassword().equals(oldPassword)){
					admin.setPassword(newPasswordVar);
					adminDao.updateAdmin(admin);
				}else{
					throw new ApplicationException("原密码输入错误！");
				}
			}
		}catch (SQLException e) {
			throw new ApplicationException("修改管理员密码时发生数据库操作错误！");
		}
		return true;
	}
	
	/**
	 * 管理员信息
	 * @param adminId 管理员id
	 * @return
	 */
	public AdminBean findAdminById(String adminId) throws Exception {
		AdminBean admin = null;
		try{
			admin = adminDao.findAdminById(adminId);
			if(!RefParaValue.empty(admin)){ 
				return admin;
			}
		}catch (SQLException e) {
			throw new ApplicationException("根据id查询管理员信息时发生数据库操作错误！");
		}
		return admin;
	}
	
	/**
	 * 删除管理员
	 * @param deleteId 管理员id
	 * @return
	 */
	public boolean deleteAdmin(String deleteId) throws Exception {
		AdminBean admin = null;
		try{
			admin = adminDao.findAdminById(deleteId);
			if(!RefParaValue.empty(admin)){ 
				adminDao.delete(admin);
			}
		}catch (SQLException e) {
			throw new ApplicationException("根据id查询管理员信息时发生数据库操作错误！");
		}
		return true;
	}
	
	/**
	 * 管理员信息修改
	 * @param admin 管理员对象
	 * @return
	 */
	public AdminBean updateAdminInfo(AdminBean admin) throws Exception {
		AdminBean adminNew = null;
		try{
			adminNew = adminDao.findAdminById(admin.getId());
			if(!RefParaValue.empty(adminNew)){ 
				adminNew.setRealName(admin.getRealName());
				adminNew.setSex(admin.getSex());
				adminNew.setBirthday(admin.getBirthday());
				adminNew.setNotes(admin.getNotes());
			}
			adminDao.updateAdmin(adminNew);
		}catch (SQLException e) {
			throw new ApplicationException("修改管理员信息时发生数据库操作错误！");
		}
		return adminNew;
	}

	public List<?> findAdminList(Page page, String findUsername) throws Exception {
		List<?> list = null;
		try {
			list =  adminDao.findBackAdminList(page, findUsername);
		} catch (SQLException e) {
			throw new ApplicationException("返回所有用户列表上时发生数据库操作错误！");
		}
		return list;
	}
	
	/**
	 * 修改管理员登录状态
	 * @param admin 管理员对象
	 * @return
	 */
	public boolean updateLogoutStatus(AdminBean admin) throws Exception {
		try {
			AdminBean adminNew  = adminDao.findAdminById(admin.getId());
			adminNew.setIsOnline(0);
			adminNew.setLastSignTime(adminNew.getThisSignTime());
			adminDao.updateAdmin(adminNew);
		} catch (SQLException e) {
			throw new ApplicationException("更新管理员注销状态时发生数据库操作错误！");
		}
		return true;
	}

	/**
	 * 分页返回管理员列表
	 * @param page 分页对象
	 * @return
	 */
	public List<AdminBean> findAdminPaging(Page page) throws Exception {
		List<AdminBean> list = null;
		try {
			list =  adminDao.findAdminPaging(page);
		} catch (SQLException e) {
			throw new ApplicationException("分页返回管理员列表时发生数据库操作错误！");
		}
		return list;
	}
	
	
	
}
